﻿/*********************************************************
 * Copyright(c) 2012-2015
 * Author:wangzhonghai
 * description：
 *       1、
 * history：
 *       1、wangzhonghai - 2015/5/5 10:29:34 - Add 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AopAndIocDemo.Biz.Aspect
{
    /// <summary>
    /// 日志类
    /// </summary>
    public static class LoggHelper
    {
        //日志文件所在路径
        private static string _logPath = string.Empty;
        //日志前缀说明信息
        private static string _logFielPrefix = string.Empty;

        /// <summary>
        /// 保存日志的文件夹
        /// </summary>
        public static string LogPath
        {
            get
            {
                if (_logPath == string.Empty)
                {
                    //e:\visual studio 2013\Projects\AopAndIocDemo\AopAndIocDemo.WebUI\
                    _logPath = AppDomain.CurrentDomain.BaseDirectory+"logs\\";
                }
                return _logPath;
            }
            set { _logPath = value; }
        }



        /// <summary>
        /// 日志文件前缀
        /// </summary>
        public static string LogFielPrefix
        {
            get { return _logFielPrefix; }
            set { _logFielPrefix = value; }
        }

        /// <summary>
        /// 写日志
        /// <param name="logType">日志类型</param>
        /// <param name="msg">日志内容</param> 
        /// </summary>
        public static void WriteLog(string logType, string msg)
        {
            System.IO.StreamWriter sw = null;
            try
            {
                //同一天同一类日志以追加形式保存
                sw =
                    System.IO.File.AppendText(LogPath + LogFielPrefix + "_" + DateTime.Now.ToString("yyyyMMdd") + ".Log");
                sw.WriteLine(logType + "#" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + msg);
            }
            finally
            {
                if (sw != null) sw.Close();
            }
        }

        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteLog(LogType logType, string msg)
        {
            WriteLog(logType.ToString(), msg);
        }
    }
}

/// <summary>
/// 日志类型
/// </summary>
public enum LogType
{
    Info,
    Debug,
    Trace, //堆栈跟踪信息
    Warning, //警告信息
    Error, //错误信息应该包含对象名、发生错误点所在的方法名称、具体错误信息
    Sql //与数据库相关的信息
}